//
//  GoodCategoryController.m
//  WuJieLive
//
//  Created by iMac on 2017/11/14.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "GoodCategoryController.h"
#import "GoodsCollectionCell.h"
#import "GoodsCategoryHeaderView.h"
#import "GoodsDetailController.h"

@interface GoodCategoryController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic , strong) UITableView * tableview;
@property (nonatomic , strong) UICollectionView * collectionview;
@property (nonatomic , strong) NSMutableArray * tableArray ;
@property (nonatomic , strong) NSMutableArray * collectionArray;
@property (nonatomic , assign) NSInteger selectedIndex;

@end

@implementation GoodCategoryController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)setNavi
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 100, 32)];
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = 5.0f;
    view.backgroundColor = Color(245, 245, 245);
    UIButton * searchBtn = [[UIButton alloc]init];
    searchBtn.frame = CGRectMake(10, 0, kScreenWidth - 120, 32);
    [searchBtn setTitle:@"搜索" forState:(UIControlStateNormal)];
    [searchBtn setImage:Image(@"icon_nav_搜索") forState:(UIControlStateNormal)];
    [searchBtn setTitleColor:TextLessImportColor forState:(UIControlStateNormal)];
    searchBtn.titleLabel.font = FontSize(13);
    searchBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
    [view addSubview:searchBtn];
    
    [searchBtn addTarget:self action:@selector(search) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.titleView = view;
    
}


- (void)setUI
{
    self.selectedIndex = 0;
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
    [self.view addSubview:view];
    view.backgroundColor = TextLessImportColor;
    
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 1, 100, kScreenHeight) style:(UITableViewStylePlain)];
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableview];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 1;
    layout.itemSize = CGSizeMake((kScreenWidth - 100) / 3.1, 100);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.tableview.frame), 1, kScreenWidth - self.tableview.width, kScreenHeight - 64) collectionViewLayout:layout];
    [self.collectionview registerNib:[UINib nibWithNibName:@"GoodsCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"GoodsCollectionCell"];
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.backgroundColor = [UIColor whiteColor];
    [self.collectionview registerNib:[UINib nibWithNibName:@"GoodsCategoryHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"GoodsCategoryHeaderView"];
    
}

-(void)setData
{
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@index/goods-categories",BaseUrl] para:nil success:^(BOOL isSuccess, id responseObject) {
        if (isSuccess) {
            self.tableArray = responseObject[@"data"];
            [self.tableview reloadData];
            NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.tableview selectRowAtIndexPath:index animated:YES scrollPosition:(UITableViewScrollPositionNone)];
            [self.view addSubview:self.collectionview];
        }
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark collectionDataSource Delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [self.tableArray[self.selectedIndex][@"son"] count];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if ([self.tableArray[self.selectedIndex][@"son"][section][@"son"] isKindOfClass:[NSArray class]]) {
        return [self.tableArray[self.selectedIndex][@"son"][section][@"son"] count];
    }else
    {
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GoodsCollectionCell" forIndexPath:indexPath];
    [cell.topImage sd_setImageWithURL:[NSURL URLWithString:self.tableArray[self.selectedIndex][@"son"][indexPath.section][@"son"][indexPath.row][@"category_picture"] ] placeholderImage:PlaceImage];
    cell.bottomLabel.text = self.tableArray[self.selectedIndex][@"son"][indexPath.section][@"son"][indexPath.row][@"category_name"];
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    
    GoodsCategoryHeaderView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"GoodsCategoryHeaderView" forIndexPath:indexPath];
    headView.contentLabel.text = self.tableArray[self.selectedIndex][@"son"][indexPath.section][@"category_name"];
    
    return headView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(kScreenWidth - 100, 30);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    GoodsDetailController * vc = [[GoodsDetailController alloc]init];
    vc.goodsID = @"17";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark tableviewDataSource Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndex = indexPath.row;
    [self.collectionview reloadData];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = Color(245, 245, 245);
    cell.textLabel.text = self.tableArray[indexPath.row][@"category_name"];
    cell.textLabel.font = FontSize(15);
    cell.selectedBackgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 55)];
    UIView * redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 2, 55)];
    redView.backgroundColor = [UIColor redColor];
    [cell.selectedBackgroundView addSubview:redView];
    cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 53, 100, 2)];
    view.backgroundColor = [UIColor whiteColor];
    [cell.contentView addSubview:view];
    
    return cell;
}

#pragma mark 搜索方法
- (void)search
{
    SearchViewController * vc = [[SearchViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(NSMutableArray *)tableArray
{
    if (!_tableArray) {
        _tableArray = [NSMutableArray array];
    }
    return _tableArray;
}

-(NSMutableArray *)collectionArray
{
    if (!_collectionArray) {
        _collectionArray = [NSMutableArray array];
    }
    return _collectionArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
